package cn.com.elite;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

public class HTBean {
	private static final Log log=LogFactory.getLog(HTBean.class);
	private DataSource dataSource;
    private DataSourceTransactionManager tran;
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public DataSourceTransactionManager getTran() {
		return tran;
	}
	public void setTran(DataSourceTransactionManager tran) {
		this.tran = tran;
	}
	private String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw=new StringWriter();  
            e.printStackTrace(new PrintWriter(sw,true));  
            str=sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	public int update_sql(String sql){
		int result=0;
		try{
			JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
			result=jdbcTemplate.update(sql);
		}catch(Exception e){
			log.error("chaxun_sql_ERRO:"+getERRO(e));
		}
		log.info(sql+":"+result);
		return result;
	}
	public List chaxun_sql(String sql){
		List result=null;
		try{
			JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
			result=jdbcTemplate.queryForList(sql);
		}catch(Exception e){
			log.error("chaxun_sql_ERRO:"+getERRO(e));
		}
		return result;
	}
    public String chaxunString(String sql){       
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String resultList="";
        try{
        	resultList=(String)jdbcTemplate.queryForObject(sql,java.lang.String.class);
        }catch(Exception e){
        	log.info(sql+":"+getERRO(e));
        }
        return resultList;
   }
    public String HTPay_log(String name1,String name2,String name3,String name4) {
    	log.info("---------HTPay_log------------");
    	final String a=name1;
    	final String b=name2;
    	final String c=name3;
    	final String d=name4;
    	log.info("a:"+a);
    	log.info("b:"+b);
    	log.info("c:"+c);
    	log.info("d:"+d);
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){
                    String storedProc = "{call HTPay_log(?,?,?,?,?)}";// 调用的sql
                    CallableStatement cs = null;
                    try{
                   	 cs = con.prepareCall(storedProc);
                   }catch(Exception e){
                   	log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                   try{
                   	cs.setString(1, a);// 设置输入参数的值
                   	cs.setString(2, b);// 设置输入参数的值
                   	cs.setString(3, c);// 设置输入参数的值
                   	cs.setString(4, d);// 设置输入参数的值
                   	cs.setInt(5, 0);// 设置输入参数的值
                   }catch(Exception e){
                   	log.info("设置输入参数的值错误:"+e);
                   }
                   try{
                   	cs.registerOutParameter(5, Types.INTEGER);// 注册输出参数的类型
                   }catch(Exception e){
                   	log.info("注册输出参数的类型错误:"+e);
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+e);
                    }
                    try{
                    	   return cs.getInt(5);// 获取输出参数的值
                    }catch(Exception e){
                    	log.info("cs.getString(5):"+e);
                    	return null;
                    }
            }
        }).toString();
        log.info("HTPay_log_param2Value:"+param2Value);
        return param2Value;
    }
    
    public String HTPay_1(String name1,String name2,String name3,String name4,String name5,String name6,String name7) {
    	log.info("---------HTPay_1------------");
    	final String a=name1;
    	final String b=name2;
    	final String c=name3;
    	final String d=name4;
    	final String e=name5;
    	final String f=name6;
    	final String g=name7;
    	log.info("a:"+a);
    	log.info("b:"+b);
    	log.info("c:"+c);
    	log.info("d:"+d);
    	log.info("e:"+e);
    	log.info("f:"+f);
    	log.info("g:"+g);
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){
                    String storedProc = "{call HTPay_1(?,?,?,?,?,?,?,?)}";// 调用的sql
                    CallableStatement cs = null;
                    try{
                   	 cs = con.prepareCall(storedProc);
                   }catch(Exception e){
                   	log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                   try{
                   	cs.setString(1, a);// 设置输入参数的值
                   	cs.setString(2, b);// 设置输入参数的值
                   	cs.setString(3, c);// 设置输入参数的值
                   	cs.setString(4, d);// 设置输入参数的值
                   	cs.setString(5, e);// 设置输入参数的值
                   	cs.setString(6, f);// 设置输入参数的值
                   	cs.setString(7, g);// 设置输入参数的值
                   	cs.setInt(8, 0);// 设置输入参数的值
                   }catch(Exception e){
                   	log.info("设置输入参数的值错误:"+e);
                   }
                   try{
                   	cs.registerOutParameter(8,Types.INTEGER);// 注册输出参数的类型
                   }catch(Exception e){
                   	log.info("注册输出参数的类型错误:"+e);
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+e);
                    }
                    try{
                    	   return cs.getInt(8);// 获取输出参数的值
                    }catch(Exception e){
                    	log.info("cs.getString(5):"+e);
                    	return null;
                    }
            }
        }).toString();
        log.info("HTPay_1_param2Value:"+param2Value);
        return param2Value;
    }
    
    public String HTPay_2(String name1,String name2,String name3,String name4,String name5,String name6,String name7,String name8,String name9,String name10) {
    	log.info("---------HTPay_2------------");
    	final String a=name1;
    	final String b=name2;
    	final String c=name3;
    	final String d=name4;
    	final String e=name5;
    	final String f=name6;
    	final String g=name7;
    	final String h=name8;
    	final String j=name9;
    	final String m=name10;
    	log.info("a:"+a);
    	log.info("b:"+b);
    	log.info("c:"+c);
    	log.info("d:"+d);
    	log.info("e:"+e);
    	log.info("f:"+f);
    	log.info("g:"+g);
    	log.info("h:"+h);
    	log.info("j:"+j);
    	log.info("m:"+m);
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){
                    String storedProc = "{call HTPay_2(?,?,?,?,?,?,?,?,?,?,?)}";// 调用的sql
                    CallableStatement cs = null;
                    try{
                   	 cs = con.prepareCall(storedProc);
                   }catch(Exception e){
                   	log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                   try{
                   	cs.setString(1, a);// 设置输入参数的值
                   	cs.setString(2, b);// 设置输入参数的值
                   	cs.setString(3, c);// 设置输入参数的值
                   	cs.setString(4, d);// 设置输入参数的值
                   	cs.setString(5, e);// 设置输入参数的值
                   	cs.setString(6, f);// 设置输入参数的值
                   	cs.setString(7, g);// 设置输入参数的值
                   	cs.setString(8, h);// 设置输入参数的值
                   	cs.setInt(9, 0);// 设置输入参数的值
                   	cs.setString(10, j);// 设置输入参数的值
                   	cs.setString(11, m);// 设置输入参数的值
                   }catch(Exception e){
                   	log.info("设置输入参数的值错误:"+e);
                   }
                   try{
                   	cs.registerOutParameter(9,Types.INTEGER);// 注册输出参数的类型
                   }catch(Exception e){
                   	log.info("注册输出参数的类型错误:"+e);
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+e);
                    }
                    try{
                    	   return cs.getInt(9);// 获取输出参数的值
                    }catch(Exception e){
                    	log.info("cs.getString(9):"+e);
                    	return null;
                    }
            }
        }).toString();
        log.info("HTPay_2_param2Value:"+param2Value);
        return param2Value;
    }
    
    
    public String HTPay_3(String name1,String name2,String name3,String name4,String name5,String name6) {
    	log.info("---------HTPay_1------------");
    	final String a=name1;
    	final String b=name2;
    	final String c=name3;
    	final String d=name4;
    	final String e=name5;
    	final String f=name6;

    	log.info("a:"+a);
    	log.info("b:"+b);
    	log.info("c:"+c);
    	log.info("d:"+d);
    	log.info("e:"+e);
    	log.info("f:"+f);
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){
                    String storedProc = "{call HTPay_3(?,?,?,?,?,?,?)}";// 调用的sql
                    CallableStatement cs = null;
                    try{
                   	 cs = con.prepareCall(storedProc);
                   }catch(Exception e){
                   	log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                   try{
                   	cs.setString(1, a);// 设置输入参数的值
                   	cs.setString(2, b);// 设置输入参数的值
                   	cs.setString(3, c);// 设置输入参数的值
                   	cs.setString(4, d);// 设置输入参数的值
                   	cs.setInt(5, 0);// 设置输入参数的值
                   	cs.setString(6, e);// 设置输入参数的值
                   	cs.setString(7, f);// 设置输入参数的值
                   }catch(Exception e){
                   	log.info("设置输入参数的值错误:"+e);
                   }
                   try{
                   	cs.registerOutParameter(5,Types.INTEGER);// 注册输出参数的类型
                   }catch(Exception e){
                   	log.info("注册输出参数的类型错误:"+e);
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+e);
                    }
                    try{
                    	   return cs.getInt(5);// 获取输出参数的值
                    }catch(Exception e){
                    	log.info("cs.getString(5):"+e);
                    	return null;
                    }
            }
        }).toString();
        log.info("HTPay_3_param2Value:"+param2Value);
        return param2Value;
    }
    
    public String HTPay_idcheck(String GUID,String merOrderNo,String PmReference) {
    	log.info("---------HTPay_idcheck------------");
    	final String a=GUID;
    	final String b=merOrderNo;
    	final String c=PmReference;

    	log.info("GUID:"+a);
    	log.info("merOrderNo:"+b);
    	log.info("PmReference:"+c);
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){
                    String storedProc = "{call HTPay_idcheck(?,?,?,?)}";// 调用的sql
                    CallableStatement cs = null;
                    try{
                   	 cs = con.prepareCall(storedProc);
                   }catch(Exception e){
                   	log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                   try{
                   	cs.setString(1,a);// 设置输入参数的值
                   	cs.setString(2,b);// 设置输入参数的值
                   	cs.setString(3,c);// 设置输入参数的值
                   	cs.setInt(4,0);// 设置输入参数的值
                   }catch(Exception e){
                   	log.info("设置输入参数的值错误:"+e);
                   }
                   try{
                   	cs.registerOutParameter(4,Types.INTEGER);// 注册输出参数的类型
                   }catch(Exception e){
                   	log.info("注册输出参数的类型错误:"+e);
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+e);
                    }
                    try{
                    	   return cs.getInt(4);// 获取输出参数的值
                    }catch(Exception e){
                    	log.info("cs.getString(4):"+e);
                    	return null;
                    }
            }
        }).toString();
        log.info("HTPay_idcheck_param2Value:"+param2Value);
        return param2Value;
    }
}
